package com.cn.mmd.controller;

import com.cn.mmd.model.Course;
import com.cn.mmd.model.CourseWeekly;
import com.cn.mmd.model.TeacherData;
import com.cn.mmd.service.CourseService;
import com.cn.mmd.service.CourseWeeklyService;
import com.cn.mmd.tools.SplitSpring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程
 * Created by Administrator on 2016/6/29 0029.
 */

@Controller
@RequestMapping(value ="page/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private CourseWeeklyService courseWeeklyService;

    private SplitSpring splitSpring =new SplitSpring();

    @Autowired
    private TokenController tokenController;
    /**
     *查看课程
     * @return
     */
    @RequestMapping(value = "/select")
    public synchronized @ResponseBody
    Object selectCourse(HttpServletRequest request){
        Map<Object,Object> map1 = new HashMap<Object,Object>();
        List<Map>  mapList = new ArrayList<Map>();
//        Integer weekly = Integer.parseInt(request.getParameter("weekly"));
        String teacherId = request.getParameter("teacherId");
        String token= request.getParameter("token");
        TeacherData teacherData;
        teacherData = (TeacherData) tokenController.token(teacherId,token);
        if (teacherData!=null) {
            String id;
            String week1;
            int n;
            int m;
            List<CourseWeekly> courseWeekly;
            Map<String, Object> map;
            int i = 0;
            List<Course> list = courseService.selectByTeacher(teacherId);
            if (list != null && list.size() > 0) {
                for (; i < list.size(); i++) {
                    id = list.get(i).getCourseid();
                    courseWeekly = courseWeeklyService.selectList(id);
                    for (int j = 0; j < courseWeekly.size(); j++) {
                        week1 = courseWeekly.get(j).getCourseweeklyweek();
                        if (!week1.equals("")) {
                            map = new HashMap<String, Object>();
                            map.put("weeklyDay", courseWeekly.get(j).getCourseweeklyday());
                            map.put("weeklyQuarter", courseWeekly.get(j).getCourseweeklyquarter());
                            map.put("beginEndWeek", week1);
                            map.put("classRoom", courseWeekly.get(j).getCourseweeklyclassroom());
                            map.put("courseName", list.get(i).getCoursename());
                            map.put("courseId", list.get(i).getCourseid());
                            map.put("courseWeeklyId", courseWeekly.get(j).getCourseweeklyid() + "");
                            mapList.add(map);
//                        }
                        }
                    }
                }
                map1.put("code", 200);
                map1.put("message", "成功");
                map1.put("data", mapList);
            } else {
                map1.put("code", 201);
                map1.put("message", "当前老师没有课程");
            }
        }else {
            map1.put("code", 202);
            map1.put("message", "此用户没有权限做此操作");
        }
        return map1;
    }
}
